﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Search.BLL
{
    public class HomeService : BaseService, IHomeService
    {
        private IHomeDAO homeDao = null;
        ICategoryDAO categoryDAO = null;
        public HomeService(IHomeDAO homeDao, ICategoryDAO categoryDAO)
        {
            this.homeDao = this.generator.CreateInterfaceProxyWithTarget<IHomeDAO>(homeDao, new IInterceptor[] { new LogInterceptor() });
            this.categoryDAO = this.generator.CreateInterfaceProxyWithTarget<ICategoryDAO>(categoryDAO, new IInterceptor[] { new LogInterceptor() });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig()
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                result = homeDao.GetHomepageConfig();
                if (result != null)
                {
                    result = result.Where(c => c.IsPub == 1).ToList();
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig(ShowLocation location, PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                result = homeDao.GetHomepageConfig(option);
                if (result != null)
                {
                    result = result.Where(c => c.IsPub == 1 && c.Location == (int)location).ToList();
                    foreach (SYS_CMS_NEWS_NEWSCONTENTCONFIG item in result)
                    {
                        SYS_CMS_DATABASE obj = categoryDAO.GetDatabaseByID(item.DbID);
                        if (obj != null)
                        {
                            item.DBName = obj.DatabaseCode;
                            item.DBDisplayName = obj.DatabaseName;
                            item.DBType = obj.DatabaseType.ToString();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig(ShowType type)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                result = homeDao.GetHomepageConfig();
                if (result != null)
                {
                    result = result.Where(c => c.IsPub == 1 && c.ShowType == (int)type).ToList();
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_JUMPTEMPLATE> GetHomepageJumpConfig(ShowLocation location, PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> result = new List<SYS_CMS_NEWS_JUMPTEMPLATE>();
            try
            {
                result = homeDao.GetHomepageJumpConfig(option);
                if (result != null)
                {
                    result = result.Where(c => c.IsPub == "1" && c.Location == (int)location).ToList();
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
